/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Supporter;

/**
 *
 * @author Van persie
 */
import java.io.InputStream;
import java.util.Stack;
import java.util.Vector;
import javax.microedition.lcdui.*;
public class ImageSupporter {
    public static Image transparent(Image img)
    {
        //image = Image.createImage(pathImg); 
        
        int width = img.getWidth();
        int height = img.getHeight();
        int rgbData[] = new int[width*height] ;
        img.getRGB(rgbData, 0, width, 0, 0, width, height);
        
        int colorkey = rgbData[1];
        for (int i = 0; i < rgbData.length; i++) { 
            //set the image to full transparency 
            if(rgbData[i] == colorkey)
                rgbData[i] &= 0x00ffffff; 
        }      
        Image transImg =  Image.createRGBImage(rgbData, width, height, true);
        return transImg;
    }
    public static Image alphaBlend(Image img)
    {
        int width = img.getWidth();
        int height = img.getHeight();
        int rgbData[] = new int[width*height] ;
        img.getRGB(rgbData, 0, width, 0, 0, width, height);
        
        int colorkey = rgbData[1];
        for (int i = 0; i < rgbData.length; i++) { 
            //set the image to full transparency 
            rgbData[i] &= 0x00ffffff; 
            rgbData[i] |= 0xaa000000;
        }      
        Image transImg =  Image.createRGBImage(rgbData, width, height, true);
        return transImg;
    }
    public static Image transparent(String PathImg)
    {
        Image image = null;
        
        try{
        Image tempPNG = Image.createImage(PathImg);        
        int  width = tempPNG.getWidth();
        int height = tempPNG.getHeight();
        int rgbData[] = new int[width*height] ;
        tempPNG.getRGB(rgbData, 0, width, 0, 0, width, height);
        
        int colorkey = rgbData[1];
        for (int i = 0; i < rgbData.length; i++) { 
            //set the image to full transparency 
            if(rgbData[i] == colorkey)
                rgbData[i] &= 0x00111111; 
        }
        
        image = Image.createRGBImage(rgbData, width, height, true);
        }catch(Exception e ){}
        return image;
    }
    public static Image resizeImage(Image image, int desWidth,int desHeight)  
    {  
       int sourceWidth = image.getWidth();  
       int sourceHeight = image.getHeight();  
       int thumbWidth = desWidth;  
       int thumbHeight = desHeight;  

       if (thumbHeight == -1)  
          thumbHeight = thumbWidth * sourceHeight / sourceWidth;  

       Image thumb = Image.createImage(thumbWidth, thumbHeight);  
       Graphics g = thumb.getGraphics();  

       for (int y = 0; y < thumbHeight; y++)  
       {  
          for (int x = 0; x < thumbWidth; x++)  
          {  
            g.setClip(x, y, 1, 1);  
            int dx = x * sourceWidth / thumbWidth;  
            int dy = y * sourceHeight / thumbHeight;  
            g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);  
          }  
       }  

       Image immutableThumb = Image.createImage(thumb);  

       return immutableThumb;  
    }  
    public static Image scaleImage(Image original, int newWidth, int newHeight)
    {        
        int[] rawInput = new int[original.getHeight() * original.getWidth()];
        original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
        
        int[] rawOutput = new int[newWidth*newHeight];        

        // YD compensates for the x loop by subtracting the width back out
        int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth(); 
        int YR = original.getHeight() % newHeight;
        int XD = original.getWidth() / newWidth;
        int XR = original.getWidth() % newWidth;        
        int outOffset= 0;
        int inOffset=  0;
        
        for (int y= newHeight, YE= 0; y > 0; y--) {            
            for (int x= newWidth, XE= 0; x > 0; x--) {
                rawOutput[outOffset++]= rawInput[inOffset];
                inOffset+=XD;
                XE+=XR;
                if (XE >= newWidth) {
                    XE-= newWidth;
                    inOffset++;
                }
            }            
            inOffset+= YD;
            YE+= YR;
            if (YE >= newHeight) {
                YE -= newHeight;     
                inOffset+=original.getWidth();
            }
        }               
        return Image.createRGBImage(rawOutput, newWidth, newHeight,true );        
    }
    public Image getImage(String pathFile)
    {
        InputStream InStream = this.getClass().getResourceAsStream(pathFile);
        Image temp = null;
        try {          
            temp = Image.createImage(InStream);
        } catch (Exception e){ }
        return temp;
    }
    
    public static Image rotateImage(Image src, float sinrads, float cosrads) {
        int sw = src.getWidth();
        int sh = src.getHeight();
        int[] srcData = new int[sw * sh];
        Image dst;


        src.getRGB(srcData, 0, sw, 0, 0, sw, sh);
        int[] dstData = new int[sw * sh];

        float sa = sinrads;
        float ca = cosrads;
        int isa = (int) (256 * sa);
        int ica = (int) (256 * ca);

        int newHeight = (int) (sw * sa);
        int newWidth = (int) (sh * sa);

        int my = -(sh >> 1);
        for (int i = 0; i < sh; i++) {
            int mx = -(sw >> 1);
            for (int j = 0; j < sw; j++) {
                int srcx = (mx * ica + my * isa) >> 8;
                int srcy = (-mx * isa + my * ica) >> 8;

                srcx += sw >> 1;
                srcy += sh >> 1;

                if (srcx < 0) {
                    srcx = 0;
                }
                if (srcy < 0) {
                    srcy = 0;
                }
                if (srcx > sw - 1) {
                    srcx = sw - 1;
                }
                if (srcy > sh - 1) {
                    srcy = sh - 1;
                }

                dstData[j + i * sw] = srcData[srcx + srcy * sw];

                mx++;
            }
            my++;
        }
       // return Image.createRGBImage(dstData, newWidth, newHeight, true);
        return Image.createRGBImage(dstData, sw, sh, true);
    }

    public static Image rotateImage(Image src, float angle) {
        int sw = src.getWidth();
        int sh = src.getHeight();
        int[] srcData = new int[sw * sh];

        src.getRGB(srcData, 0, sw, 0, 0, sw, sh);
        int[] dstData = new int[sw * sh];

        double rads = angle * Math.PI / 180.f;
        float sa = (float) Math.sin(rads);
        float ca = (float) Math.cos(rads);
        int isa = (int) (256 * sa);
        int ica = (int) (256 * ca);

        int my = -(sh >> 1);
        for (int i = 0; i < sh; i++) {
            int mx = -(sw >> 1);
            for (int j = 0; j < sw; j++) {
                int srcx = (mx * ica + my * isa) >> 8;
                int srcy = (-mx * isa + my * ica) >> 8;

                srcx += sw >> 1;
                srcy += sh >> 1;

                if (srcx < 0) {
                    srcx = 0;
                }
                if (srcy < 0) {
                    srcy = 0;
                }
                if (srcx > sw - 1) {
                    srcx = sw - 1;
                }
                if (srcy > sh - 1) {
                    srcy = sh - 1;
                }

                dstData[j + i * sw] = srcData[srcx + srcy * sw];

                mx++;
            }
            my++;
        }
        
        return Image.createRGBImage(dstData, sw, sh, true);
    }
    
}
